package handlers

import (
	"art-management-system/models"
	"art-management-system/pkg"
	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")

	if tel == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.User{
		Tel:      tel,
		Password: password,
	}

	err := models.DB.Where("tel=?", tel).First(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	token := pkg.TokenHandler(int(data.ID))
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "登录成功",
		"token": token,
	})
	return
}
